﻿using System;
using UnityEngine;
using XUtliPoolLib;

namespace XMainClient
{
	// Token: 0x02000FE1 RID: 4065
	internal class XShadowComponent : XComponent
	{
		// Token: 0x170036DC RID: 14044
		// (get) Token: 0x0600D33C RID: 54076 RVA: 0x003180E0 File Offset: 0x003162E0
		public override uint ID
		{
			get
			{
				return XShadowComponent.uuID;
			}
		}

		// Token: 0x170036DD RID: 14045
		// (get) Token: 0x0600D33D RID: 54077 RVA: 0x003180F8 File Offset: 0x003162F8
		private XGameObject Shadow
		{
			get
			{
				return this._shadow;
			}
		}

		// Token: 0x0600D33E RID: 54078 RVA: 0x00318110 File Offset: 0x00316310
		private static void _Init(XGameObject gameObject, object o, int commandID)
		{
			XShadowComponent xshadowComponent = o as XShadowComponent;
			GameObject gameObject2 = gameObject.Get();
			Renderer componentInChildren = gameObject2.GetComponentInChildren<Renderer>();
			componentInChildren.enabled = (!xshadowComponent.Entity.IsDisappear && xshadowComponent.Entity.IsVisible);
			componentInChildren.gameObject.layer = xshadowComponent._entity.DefaultLayer;
			XRenderComponent.AddShadowObj(xshadowComponent._entity, gameObject2, componentInChildren);
		}

		// Token: 0x0600D33F RID: 54079 RVA: 0x0031817C File Offset: 0x0031637C
		private void LoadShadow()
		{
			bool flag = this._shadow == null;
			if (flag)
			{
				this._shadow = XGameObject.CreateXGameObject("Prefabs/Shadow", true, true);
				this._shadow.SetParent(this._entity.MoveObj);
				this._shadow.SetLocalPRS(Vector3.zero, true, Quaternion.identity, true, Vector3.one, false);
				this._shadow.CallCommand(XShadowComponent._initCb, this, -1, false);
			}
		}

		// Token: 0x0600D340 RID: 54080 RVA: 0x003181F4 File Offset: 0x003163F4
		private void ProcessRealtimeShadow()
		{
			this._showRealTimeShadow = false;
			bool flag = this._entity != null;
			if (flag)
			{
				this._showRealTimeShadow = this._entity.ProcessRealTimeShadow();
			}
		}

		// Token: 0x0600D341 RID: 54081 RVA: 0x0031822C File Offset: 0x0031642C
		private void ProcessFakeShadow()
		{
			bool flag = XEntity.ValideEntity(this._entity.Transformee);
			bool flag2;
			if (flag)
			{
				flag2 = this._entity.Transformee.CastFakeShadow();
			}
			else
			{
				flag2 = this._entity.CastFakeShadow();
			}
			bool enable = !this._showRealTimeShadow && flag2;
			bool flag3 = !this._showRealTimeShadow && flag2;
			if (flag3)
			{
				this.LoadShadow();
			}
			bool flag4 = this._shadow != null;
			if (flag4)
			{
				this._shadow.SetActive(enable, "");
			}
		}

		// Token: 0x0600D342 RID: 54082 RVA: 0x003182B8 File Offset: 0x003164B8
		public void ProcessShadow()
		{
			this.ProcessRealtimeShadow();
			this.ProcessFakeShadow();
		}

		// Token: 0x0600D343 RID: 54083 RVA: 0x003182C9 File Offset: 0x003164C9
		protected override void EventSubscribe()
		{
			base.RegisterEvent(XEventDefine.XEvent_OnMounted, new XComponent.XEventHandler(this.OnMountEvent));
			base.RegisterEvent(XEventDefine.XEvent_OnUnMounted, new XComponent.XEventHandler(this.OnMountEvent));
		}

		// Token: 0x0600D344 RID: 54084 RVA: 0x003182FC File Offset: 0x003164FC
		public override void Attached()
		{
			base.Attached();
			this.ProcessShadow();
			bool showRealTimeShadow = this._showRealTimeShadow;
			if (!showRealTimeShadow)
			{
				bool flag = this._shadow != null;
				if (flag)
				{
					Vector3 localScale = Vector3.one * this._entity.Radius * 2.5f;
					this._shadow.LocalScale = localScale;
				}
			}
		}

		// Token: 0x0600D345 RID: 54085 RVA: 0x00318360 File Offset: 0x00316560
		public override void OnDetachFromHost()
		{
			bool flag = this._shadow != null;
			if (flag)
			{
				XRenderComponent.RemoveObj(this._entity, this._shadow.Get());
				XGameObject.DestroyXGameObject(this._shadow);
				this._shadow = null;
			}
			base.OnDetachFromHost();
		}

		// Token: 0x0600D346 RID: 54086 RVA: 0x003183B0 File Offset: 0x003165B0
		protected bool OnMountEvent(XEventArgs e)
		{
			bool showRealTimeShadow = this._showRealTimeShadow;
			bool result;
			if (showRealTimeShadow)
			{
				result = true;
			}
			else
			{
				bool flag = this._shadow != null;
				if (flag)
				{
					this._shadow.SetParent(this._entity.MoveObj);
					this._shadow.SetLocalPRS(Vector3.zero, true, Quaternion.identity, true, Vector3.one, e is XOnUnMountedEventArgs);
				}
				result = true;
			}
			return result;
		}

		// Token: 0x0600D347 RID: 54087 RVA: 0x00318420 File Offset: 0x00316620
		public override void PostUpdate(float fDeltaT)
		{
			bool showRealTimeShadow = this._showRealTimeShadow;
			if (!showRealTimeShadow)
			{
				bool flag = this._shadow != null;
				if (flag)
				{
					Vector3 zero = Vector3.zero;
					Vector3 position = this._entity.MoveObj.Position;
					zero.y = (this._entity.StandOn ? 0f : (XSingleton<XScene>.singleton.TerrainY(position) - position.y)) / this._entity.Scale + 0.025f;
					this._shadow.SetLocalPRS(zero, true, XSingleton<XCommon>.singleton.RotateToGround(position, Vector3.forward), true, Vector3.one, false);
				}
			}
		}

		// Token: 0x0600D348 RID: 54088 RVA: 0x003184C8 File Offset: 0x003166C8
		public void SetActive(bool active)
		{
			bool flag = this._shadow != null;
			if (flag)
			{
				this._shadow.SetActive(active, "");
			}
		}

		// Token: 0x04006008 RID: 24584
		public new static readonly uint uuID = XSingleton<XCommon>.singleton.XHash("Shadow");

		// Token: 0x04006009 RID: 24585
		private XGameObject _shadow = null;

		// Token: 0x0400600A RID: 24586
		private bool _showRealTimeShadow = false;

		// Token: 0x0400600B RID: 24587
		private static CommandCallback _initCb = new CommandCallback(XShadowComponent._Init);
	}
}
